﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Hosting;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 工具资源
    /// </summary>
    public class UtilityController : BaseAdminController
    {
        #region 上传图片

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<string> UploadPic()
        {
            //保存到硬盘
            var fileInfo = IOHelper.Save(AppSettings.UploadPath.fileAnnexes);
            //没有文件上传，直接返回
            if (fileInfo.IsEmpty())
            {
                return Fail<string>("");
            }
            return Success<string>(fileInfo.VirtualPath);
        }

        #endregion

        #region 预览图片

        /// <summary>
        /// 预览图片
        /// </summary>
        /// <param name="VirtualPath">文件相对路径（含后缀）</param>
        /// <returns></returns>
        [HttpGet, AllowAnonymous]
        public IActionResult PreviewPic(string VirtualPath)
        {
            var fileInfo = IOHelper.Read(VirtualPath);
            if (fileInfo.IsEmpty()) return NotFound(); 
            return File(fileInfo.FileStream, fileInfo.ContentType, fileInfo.FileName);
        }

        #endregion

        #region 富文本框

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<string> Editor_Upload()
        {
            //保存到硬盘
            var fileInfo = IOHelper.Save(AppSettings.UploadPath.fileEditor);
            //没有文件上传，直接返回
            if (fileInfo.IsEmpty())
            {
                return Fail<string>("");
            }
            return Success<string>(fileInfo.VirtualPath);
        }

        #endregion

        #region 清空缓存
        /// <summary>
        /// 清空缓存
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [AllowAnonymous]
        public ResParameter ClearRedis()
        {
            CacheHelper.Cache.RemoveAll(AppSettings.ProjectName.EName);
            return Success("服务器缓存清除成功！");
        }
        #endregion

        #region 下载导入错误标注文件
        /// <summary>
        /// 下载文件(导入文件未被导入的数据)
        /// </summary>
        /// <param name="VirtualPath">文件相对路径（含后缀）</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownImportErrorFile(string VirtualPath)
        {
            var fileInfo = IOHelper.Read(VirtualPath);
            if (fileInfo.IsEmpty()) return NotFound();
            #region 文件名
            //解决 Refused to get unsafe header "XXX"的错误
            HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "filename");
            //文件名（Url加密防止中文乱码）
            HttpContext.Response.Headers.Add("filename", HttpUtility.UrlEncode("导入错误标注.xlsx"));
            #endregion
            return File(fileInfo.FileStream, fileInfo.ContentType, fileInfo.FileName);
        }
        #endregion
    }
}
